//
//  ServiceCenterVC.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/2/5.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "ServiceCenterVC.h"
#import "ServiceCenterCell.h"
#import "AppHeader.h"
#import "WX2DCodeView.h"
#import "ZLPhotoManager.h"
#import <WXApi.h>

@interface ServiceCenterVC () <UITableViewDelegate, UITableViewDataSource, UIWebViewDelegate> {
    
    NSString *qq;
    NSString *weixin;
    NSString *phone;
    
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;


@end

@implementation ServiceCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray =@[@{@"imageName":@"qq_services", @"typeName":@"QQ客服", @"name":@"娃娃机在线QQ客服", @"number":@"QQ: "}.mutableCopy,
                      @{@"imageName":@"weixin_services", @"typeName":@"微信客服", @"name":@"娃娃机在线微信客服", @"number":@"WX:"}.mutableCopy,
                      @{@"imageName":@"share_kefu", @"typeName":@"客服电话", @"name":@"娃娃机人工客服", @"number":@"手机:"}.mutableCopy].mutableCopy;
    [self _requestNetwork];
}

#pragma mark - Request Network
- (void)_requestNetwork {
    
    [HttpUrl GET:@"sysConfig/selectCustomerService" dict:nil hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            qq = data[@"data"][@"qq"];
            weixin = data[@"data"][@"weixin"];
            phone = data[@"data"][@"phone"];
            NSArray *arr = @[[NSString stringWithFormat:@"QQ: %@", qq], [NSString stringWithFormat:@"WX: %@", weixin], [NSString stringWithFormat:@"手机: %@", phone]];
            for (int i = 0; i < self.dataArray.count; i++) {
                NSMutableDictionary *mutDic  = self.dataArray[i];
                [mutDic setObject:arr[i] forKey:@"number"];
            }
            [self.tableView reloadData];
            
        } else {
            ShowAlertTitle(data[@"msg"]);
        }
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ServiceCenterCell *cell =[tableView dequeueReusableCellWithIdentifier:@"ServiceCenterCell"];
    if (!cell) {
        cell =[ServiceCenterCell findCellWithIdentifier:@"ServiceCenterCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    NSDictionary *contentDict =self.dataArray[indexPath.row];
    cell.myImageView.image =[UIImage imageNamed:contentDict[@"imageName"]];
    cell.typeLabel.text =contentDict[@"typeName"];
    cell.nameLabel.text =contentDict[@"name"];
    cell.numberLabel.text =contentDict[@"number"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row ==0) {
        [self onOpenWPA:qq];
    } else if (indexPath.row ==1) {
        [self onOpenWX];
    } else if (indexPath.row ==2) {
        NSMutableString *str =[[NSMutableString alloc] initWithFormat:@"telprompt://%@", phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
}

/// QQ跳转零时会话
- (void)onOpenWPA:(NSString *)QQNumber {
    [AppToast showStatusWithString:nil];
    NSString *URLString =[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web", QQNumber];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSURL *url = [NSURL URLWithString:URLString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    webView.delegate = self;
    [webView loadRequest:request];
    [self.view addSubview:webView];
}
/// 跳转微信公众号
- (void)onOpenWX {
    AlertBgView *alertView =[[AlertBgView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) clickRemove:YES bgColor:[UIColor colorWithWhite:0 alpha:0.6]];
    [alertView showView];
    alertView.centerView_height.constant =350;
    
    
    WX2DCodeView *codeView =[WX2DCodeView loadNib:@"WX2DCodeView" frame:CGRectMake((KScreenWidth-300)/2.0, 0, 300, alertView.centerView_height.constant)];
    codeView.cornerRadius =5.0;
    codeView.numberLabel.text =weixin;
    [codeView.imageView sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517823822013&di=898a401104dbdde44153b14487de2d50&imgtype=0&src=http%3A%2F%2Fmicoe.com%2Fimages%2Ferweima-weixin.jpg"]];
    [codeView longPressGestureRecognizerBlock:^(UIView *view) {
        jxt_showAlertTwoButton(@"提示", @"是否保存此二维码到系统相册", @"取消", nil, @"保存", ^(NSInteger buttonIndex) {
            if (buttonIndex ==1) {
                UIImageView *imageView =(UIImageView*)view;
                [self savePhont:imageView.image];
            }
        });
    }];
    [codeView copyButtonBlock:^(UIButton *button) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        [pasteboard setString:codeView.numberLabel.text];
        [AppToast showFileWithString:@"已复制"];
    }];
    [alertView.centerView addSubview:codeView];
}
/// 保存
- (void)savePhont:(UIImage *)image {
    [PermissionTool getPhotosPermission:^(NSInteger authStatus) {
        if (authStatus ==1) {
            [ZLPhotoManager saveImageToAblum:image completion:^(BOOL suc, PHAsset *asset) {
                if (suc) {
                    [AppToast showFileWithString:@"保存到系统相册成功" OffsetY:80];
                } else {
                    ShowAlert(@"保存失败,请稍后再试", self);
                }
            }];
        } else {
            ShowAlert(@"您已拒绝访问相册权限,请在系统'设置-隐私-相机'中打开", self);
        }
    }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [AppToast dismiss];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [AppToast dismiss];
    NSString *message =[NSString stringWithFormat:@"调起会话失败(%ld)",(long) error.code];
    ShowAlertTitle(message);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
